package com.kanshu.ksgb.fastread.doudou.module.reader.utils;

import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.util.DiskLruCacheUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.SimpleChapterBean;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVDefaultManager;
import com.kanshu.ksgb.fastread.doudou.module.book.presenter.BookListPresenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关于获取书籍所有章节信息
 */
public class ObtainAllSimpleChaptersHelper {
    static Map<String, List<SimpleChapterBean>> mChaptersMap = new HashMap<>();

    /**
     * 先从内存获取章节列表，获取不到从磁盘和网络中获取
     * @param bookId
     * @param callback
     */
    public static void getAllSimpleChapters(String bookId, INetCommCallback<List<SimpleChapterBean>> callback) {
        if (mChaptersMap.get(bookId) != null) {
            if (callback != null) {
                callback.onResponse(mChaptersMap.get(bookId));
                return;
            }
        }
        List<SimpleChapterBean> chapters = DiskLruCacheUtils.getArray("all_simple_chapters@" + bookId, SimpleChapterBean.class);
        if (!Utils.isEmptyList(chapters)) {
            mChaptersMap.put(bookId, chapters);
            if (callback != null) {
                callback.onResponse(mChaptersMap.get(bookId));
                return;
            }
        }
        BookListPresenter presenter = new BookListPresenter(null);
        presenter.getAllSimpleChapters(bookId, new INetCommCallback<List<SimpleChapterBean>>() {
            @Override
            public void onResponse(List<SimpleChapterBean> simpleChapterBeans) {
                if (!Utils.isEmptyList(simpleChapterBeans)) {
                    mChaptersMap.put(bookId, simpleChapterBeans);
                    DiskLruCacheUtils.put("all_simple_chapters@" + bookId, simpleChapterBeans, false);
                    MMKVDefaultManager.getInstance().setUpdateChapterListTime(bookId);
                    if (callback != null) {
                        callback.onResponse(mChaptersMap.get(bookId));
                    }
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取所有简单章节从内存缓存和硬盘缓存中获取
     * @param bookId
     * @return
     */
    public static List<SimpleChapterBean> getAllSimpleChaptersFromCache(String bookId) {
        List<SimpleChapterBean> list = mChaptersMap.get(bookId);
        if (!Utils.isEmptyList(list)) {
            return list;
        }
        List<SimpleChapterBean> chapters = DiskLruCacheUtils.getArray("all_simple_chapters@" + bookId, SimpleChapterBean.class);
        if (!Utils.isEmptyList(chapters)) {
            mChaptersMap.put(bookId, chapters);
        }
        return mChaptersMap.get(bookId);
    }

    /**
     * 从网络中获取最新的章节列表
     * @param bookId
     */
    public static void getAllSimpleChaptersByForce(String bookId) {
        getAllSimpleChaptersByForce(bookId, false);
    }

    public static void getAllSimpleChaptersByForce(String bookId, boolean ifNeed) {
        List<SimpleChapterBean> list = mChaptersMap.get(bookId);
        if (!Utils.isEmptyList(list) && (ifNeed
                || !MMKVDefaultManager.getInstance().isNeedUpdateChapterList(bookId))) {
            return;
        }
        BookListPresenter presenter = new BookListPresenter(null);
        presenter.getAllSimpleChapters(bookId, new INetCommCallback<List<SimpleChapterBean>>() {
            @Override
            public void onResponse(List<SimpleChapterBean> simpleChapterBeans) {
                mChaptersMap.put(bookId, simpleChapterBeans);
                MMKVDefaultManager.getInstance().setUpdateChapterListTime(bookId);
                DiskLruCacheUtils.put("all_simple_chapters@" + bookId, simpleChapterBeans, true);
            }
            @Override
            public void onError(int code, String errDesc) {
            }
        });
    }


    public static void clear() {
        mChaptersMap.clear();
    }
}
